package com.reisen;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.yaml.snakeyaml.Yaml;

public class PasswordUtil {

    /**
     * =======================================测试================================================
     * @param args
     * @throws FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException {


//        decrypt("秘钥",
//                "秘钥",
//                "秘钥");


        decrypt("","i+H2E1xPBSPWqQVVSrar4A==","");
    }

    /**
     *
     * @param url 需要加密的url
     * @param username 加密的username
     * @param pwd 加密的密码
     * @return
     */
    public static List<String> Encrypt(String url,String username,String pwd){

        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword("sigtuna");                        // 加密的密钥，随便自己填写，很重要千万不要告诉别人
        standardPBEStringEncryptor.setConfig(config);
        // String url = "jdbc:mysql://xxx.xxxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&autoReconnect=true&useSSL=false";         //自己的密码
        // String root = "test";         //自己的密码
        // String pwd = "test";         //自己的密码

        System.out.println("url "+ standardPBEStringEncryptor.encrypt(url));
        System.out.println("root "+ standardPBEStringEncryptor.encrypt(username));
        System.out.println("pwd "+ standardPBEStringEncryptor.encrypt(pwd));
        return null;
    }

    /**
     *
     * @param url 解密的url
     * @param username 解密的username
     * @param pwd 解密的password
     * @return
     */
    public static List<String> decrypt(String url, String username, String pwd){

        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("sigtuna");
        standardPBEStringEncryptor.setConfig(config);
        String plainTextUrl = null;
        if (url!=null && !url.equals("")){
            plainTextUrl = standardPBEStringEncryptor.decrypt(url);
        }
        String plainTextUser = null;
        if (username!=null && !username.equals("")){
            plainTextUser = standardPBEStringEncryptor.decrypt(username);
        }
        String plainTextPwd = null;
        if (pwd!=null && !pwd.equals("")){
            plainTextPwd = standardPBEStringEncryptor.decrypt(pwd);
        }

        System.out.println("url: "+plainTextUrl);
        System.out.println("username: "+plainTextUser);
        System.out.println("password: "+plainTextPwd);
        return null;
    }

    /**
     *
     * @param filePath 需要解密的文件地址
     * @return
     * @throws FileNotFoundException
     */

    public static List<String> EaseDecrypt(String filePath) throws FileNotFoundException{

        // System.out.println("输入解密的配置文件地址");
        // Scanner scanner = new Scanner(System.in);
        // String filePath = scanner.nextLine();
        // InputStream io = new FileInputStream("xxx\\FileRecv\\application-dev.yml");
        InputStream io = new FileInputStream(filePath);
        Yaml yaml=new Yaml();
        Map<String, Object> map =yaml.load(io);
        String url = "";
        String username = "";
        String pwd = "";
//        System.out.println(map);
//        System.out.println(map.get("spring"));
//        System.out.println();

        // 获得url
        if (map.get("spring") != null && !map.get("spring").equals("")) {
            Map<String,Object> mapd = (Map<String, Object>) map.get("spring");
            if (mapd.get("datasource") != null) {
                Map<String,Object> mapdata = (Map<String, Object>) mapd.get("datasource");
                if (mapdata.get("druid") != null) {
                    Map<String,Object> mapdata1 = (Map<String, Object>) mapdata.get("druid");
                    if (mapdata1.get("master") != null) {
                        Map<String,Object> mapdata2 = (Map<String, Object>) mapdata1.get("master");
                        if (mapdata2.get("url") != null) {
                            url = (String) mapdata2.get("url");
                            username = (String) mapdata2.get("username");
                            pwd = (String) mapdata2.get("password");;
                        }
                    }
                }
            }
        }

        // 获得加密密码
        String algorithm = "";
        String password = "";
        String prefix = "";
        String suffix = "";
        if (map.get("jasypt") != null && !map.get("jasypt").equals("")) {
            Map<String,Object> mapd = (Map<String, Object>) map.get("jasypt");
            if (mapd.get("encryptor") != null) {
                Map<String,Object> mapdata = (Map<String, Object>) mapd.get("encryptor");
                algorithm = (String) mapdata.get("algorithm");
                password = (String) mapdata.get("password");


                if(mapdata.get("property")!= null){
                    Map<String,Object> mapdata1 = (Map<String, Object>) mapdata.get("property");
                    prefix = (String) mapdata1.get("prefix");
                    suffix = (String) mapdata1.get("suffix");
                }
            }
        }


        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        // 去除前缀
        url = removePro(url,prefix,suffix);
        username = removePro(username,prefix,suffix);
        pwd = removePro(pwd,prefix,suffix);

        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlgorithm(algorithm);
        config.setPassword(password);
        standardPBEStringEncryptor.setConfig(config);
        String plainTextUrl = standardPBEStringEncryptor.decrypt(url);
        String plainTextUser = standardPBEStringEncryptor.decrypt(username);
        String plainTextPwd = standardPBEStringEncryptor.decrypt(pwd);
        System.out.println("url: "+plainTextUrl);
        System.out.println("username: "+plainTextUser);
        System.out.println("password: "+plainTextPwd);
        return null;
    }


    /**
     *  去除前缀和后缀
     * @param oString
     * @param prefix
     * @param suffix
     * @return
     */

    private static String removePro(String oString,String prefix,String suffix){

        String str = oString;
        if (prefix!=null) {
            str = oString.substring(prefix.length(), oString.length());
            oString = str;
            if (suffix!=null) {
                str = oString.substring(0, oString.length()-suffix.length());
            }
        }
        
        return str;
    }

    private static List<String> getMapKey(String key , Map<String,Object> map){

        List<String> list = new ArrayList<>();
        Map<String,Object> keyMap = new HashMap<>();
        if (key != null) {
            if (map.get(key) != null) {
                keyMap = (Map<String, Object>) map.get(key);
            }else{
                return null;
            }
        }else{
            keyMap = map;
        }
        for(String x : keyMap.keySet()){

        }
        return null;
    }

} 